<template>
  <div class="common-search-form-component">
    <a-form :model="queryParams">
      <a-row :gutter="16">
        <a-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
          <a-form-item name="keyword" :label="$t('role.keyword')">
            <a-input
              v-model:value="queryParams.keyword"
              :placeholder="$t('role.searchKeywordPlaceholder')"
              allow-clear
              @pressEnter="handleSearch"
            />
          </a-form-item>
        </a-col>

        <a-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
          <a-form-item name="status" :label="$t('role.status')">
            <a-select
              v-model:value="queryParams.status"
              :placeholder="$t('role.statusPlaceholder')"
              :options="statusOptions"
              allow-clear
            />
          </a-form-item>
        </a-col>

        <a-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6">
          <a-form-item name="dateRange" :label="$t('common.dateRange')">
            <a-range-picker
              v-model:value="dateRange"
              :placeholder="[$t('common.startDate'), $t('common.endDate')]"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import type { Dayjs } from 'dayjs'

// 组件名称
defineOptions({
  name: 'RoleSearchForm'
})

// 定义组件事件
const emit = defineEmits(['search', 'reset'])

// 国际化
const { t } = useI18n()

// 状态选项
const statusOptions = [
  { value: 1, label: t('common.enabled') },
  { value: 0, label: t('common.disabled') }
]

// 日期范围
const dateRange = ref<[Dayjs, Dayjs] | undefined>(undefined)

// 查询参数
const queryParams = reactive({
  keyword: '',
  status: undefined as number | undefined,
  beginTime: '',
  endTime: ''
})

/**
 * 搜索处理
 */
const handleSearch = () => {
  // 处理日期
  if (dateRange.value) {
    const [start, end] = dateRange.value
    if (start && end) {
      queryParams.beginTime = start.format('YYYY-MM-DD')
      queryParams.endTime = end.format('YYYY-MM-DD')
    }
  }
  
  emit('search', { ...queryParams })
}

/**
 * 重置处理
 */
const handleReset = () => {
  dateRange.value = undefined
  Object.assign(queryParams, {
    keyword: '',
    status: undefined,
    beginTime: '',
    endTime: ''
  })
  emit('reset')
}
</script>

<style lang="scss" scoped>
@import '@/assets/styles/views/common.scss';
</style> 